<?php
namespace app\manage\controller;
use think\Db;

class UserList extends Base
{
    public function index(){
        if( request()->isAjax() ){
            $page = input('page');
            $limit = input('limit');
            $myUserId = input('my_user_id');
            $chUserId = input('ch_user_id');
            $phoneNumber = input('phone_number');

            $where = 1;
            !empty($myUserId) && $where .= " && (my_user_id='{$myUserId}' OR my_user_name like '%{$myUserId}%')";
            !empty($chUserId) && $where .= " && (ch_user_id='{$chUserId}' OR ch_user_name like '%{$chUserId}%')";
            !empty($phoneNumber) && $where .= " && phone_number='{$phoneNumber}'";

            if( ! $count=model('User')->where($where)->count() ){
                $this->layTable();
            }

            $select = model('User')
                ->field('my_user_id,my_user_name,channel_id,reg_cps_id,reg_game_id,reg_time,phone_number,reg_type,last_login_time,ch_user_id,ch_user_name,cp_user_id')
                ->where($where)
                ->order('reg_time desc')
                ->limit(($page-1)*$limit, $limit)
                ->select();

            $data = [];
            $gameIdName = model('Game')->gameIdName();
            $channelIdName = model('Channel')->channelIdName();
            foreach ($select as $k=>$v){
                switch ($v['reg_type']){
                    case 'name' : $regType='用户名';break;
                    case 'chuser' : $regType='联运';break;
                    case 'phone' : $regType='手机号';break;
                    case 'auto' : $regType='自动';break;
                    default: $regType = '';
                }

                if($v['channel_id']==1000){
                    $regCpsName = isset($channelIdName[ $v['reg_cps_id'] ]) ? $channelIdName[ $v['reg_cps_id'] ] : '';
                }else{
                    $regCpsName = isset($channelIdName[ $v['channel_id'] ]) ? $channelIdName[ $v['channel_id'] ] : '';
                }

                $data[] = [
                    'my_user_id' => $v['my_user_id'],
                    'my_user_name' => $v['my_user_name'],
                    'plat' =>  $v['channel_id']==1000 ? 'CPS' : 'SDK',
                    'channel_id' =>  $v['channel_id'],
                    'reg_cps_id' => $v['channel_id']==1000 ? $v['reg_cps_id']: $v['channel_id'],
                    'reg_cps_name' => $regCpsName,
                    'reg_game_id' => $v['reg_game_id'],
                    'reg_game_name' => isset($gameIdName[ $v['reg_game_id'] ]) ? $gameIdName[ $v['reg_game_id'] ] : '',
                    'reg_time' => date('Y-m-d H:i:s', $v['reg_time']),
                    'phone_number' => $v['phone_number'],
                    'reg_type' => $regType,
                    'last_login_time' => date('Y-m-d H:i:s', $v['last_login_time']),
                    'ch_user_id' => $v['ch_user_id'],
                    'ch_user_name' => $v['ch_user_name'],
                    'cp_user_id' => $v['cp_user_id'],
                ];
            }
            $this->layTable(0, '用户列表', $data, $count);
        }else{
            return view();
        }
    }

    public function pay(){
        if( request()->isAjax() ){
            $page = input('page');
            $limit = input('limit');
            $myUserId = input('my_user_id');
            $channelId = input('channel_id');

            $startTime = model('User')->where('my_user_id', $myUserId)->value('reg_time');
            $endTime = time();

            $wherePay = "pay_time between {$startTime} AND {$endTime} && debug=0 && pay_result=1 && my_user_id='{$myUserId}'";
            if($channelId==1000){
                $wherePay .= ' && channel_id=1000';
            }else{
                $wherePay .= ' && channel_id>=5000';
            }

            //跨年分表
            $y1 = date('Y', $startTime);
            $y2 = date('Y', $endTime);

            $field = "my_user_id, my_user_name, reg_cps_id, channel_id, game_id, pay_order_id, pay_time, total_price";

            if($y2-$y1 == 2){ //跟现在查两年，查三个
                $subQuery = Db::name('PayOrderY_'.$y1)
                    ->field($field)
                    ->where($wherePay)
                    ->union("select {$field} from sdk_pay_order_y_2017 where ({$wherePay})", true)
                    ->union("select {$field} from sdk_pay_order_y_{$y2} where ({$wherePay})", true)
                    ->buildSql();
                $count = Db::table($subQuery.' y')->count();
                $select = Db::table($subQuery.' y')
                    ->order('pay_time desc')
                    ->limit(($page-1)*$limit, $limit)
                    ->select();
                $sum = Db::table($subQuery.' y')->sum('total_price');
            }else if($y2-$y1 == 1){ //跟现在差一年，查两个
                $subQuery = Db::name('PayOrderY_'.$y1)
                    ->field($field)
                    ->where($wherePay)
                    ->union("select {$field} from sdk_pay_order_y_{$y2} where ({$wherePay})", true)
                    ->buildSql();
                $count = Db::table($subQuery.' y')->count();
                $select = Db::table($subQuery.' y')
                    ->order('pay_time desc')
                    ->limit(($page-1)*$limit, $limit)
                    ->select();
                $sum = Db::table($subQuery.' y')->sum('total_price');
            }else if($y1 == $y2){ //同一年查一个
                $count = Db::name('PayOrderY_'.$y1)->where($wherePay)->count();
                $select = Db::name('PayOrderY_'.$y1)
                    ->field($field)
                    ->where($wherePay)
                    ->order('pay_time desc')
                    ->limit(($page-1)*$limit, $limit)
                    ->select();
                $sum = Db::name('PayOrderY_'.$y1)->where($wherePay)->sum('total_price');
            }

            if( ! $count ){
                $this->layTable(0, '充值记录', [], 0, ['total_price'=>0.00]);
            }

            $totalPrice = $sum/100;

            $data = [];

            $gameIdName = model('Game')->gameIdName();
            $channelIdName = model('Channel')->channelIdName();
            foreach ($select as $k=>$v) {
                $data[] = [
                    'my_user_id' => $v['my_user_id'],
                    'my_user_name' => $v['my_user_name'],
                    'channel_id' => $v['channel_id']==1000 ? 'CPS' : 'SDK',
                    'reg_cps_id' => $v['channel_id']==1000 ? $channelIdName[ $v['reg_cps_id'] ] : $channelIdName[ $v['channel_id'] ],
                    'game_id' => $gameIdName[ $v['game_id'] ],
                    'pay_order_id' => $v['pay_order_id'],
                    'pay_time' => date('Y-m-d H:i:s', $v['pay_time']),
                    'total_price' => $v['total_price']/100
                ];
            }

            $this->layTable(0, '充值记录', $data, $count, ['total_price'=>$totalPrice]);
        }else{
            return view();
        }
    }

    public function game(){
        if( request()->isAjax() ){
            $page = input('page');
            $limit = input('limit');
            $myUserId = input('my_user_id');

            $startTime = model('User')->where('my_user_id', $myUserId)->value('reg_time');
            $endTime = time();

            $whereGame = "my_user_id='{$myUserId}'";

            //跨年分表
            $y1 = date('Y', $startTime);
            $y2 = date('Y', $endTime);

            $field = "my_user_id, channel_id, cps_id, game_id, bond_time";

            if($y2-$y1 == 2){ //跟现在查两年，查三个
                $subQuery = Db::name('GameUserY_'.$y1)
                    ->field($field)
                    ->where($whereGame)
                    ->union("select {$field} from sdk_game_user_y_2017 where ({$whereGame})", true)
                    ->union("select {$field} from sdk_game_user_y_{$y2} where ({$whereGame})", true)
                    ->buildSql();
                $count = Db::table($subQuery.' y')->count();
                $select = Db::table($subQuery.' y')
                    ->order('bond_time desc')
                    ->limit(($page-1)*$limit, $limit)
                    ->select();
            }else if($y2-$y1 == 1){ //跟现在差一年，查两个
                $subQuery = Db::name('GameUserY_'.$y1)
                    ->field($field)
                    ->where($whereGame)
                    ->union("select {$field} from sdk_game_user_y_{$y2} where ({$whereGame})", true)
                    ->buildSql();
                $count = Db::table($subQuery.' y')->count();
                $select = Db::table($subQuery.' y')
                    ->order('bond_time desc')
                    ->limit(($page-1)*$limit, $limit)
                    ->select();
            }else if($y1 == $y2){ //同一年查一个
                $count = Db::name('GameUserY_'.$y1)->where($whereGame)->count();
                $select = Db::name('GameUserY_'.$y1)
                    ->field($field)
                    ->where($whereGame)
                    ->order('bond_time desc')
                    ->limit(($page-1)*$limit, $limit)
                    ->select();
            }

            if( ! $count ){
                $this->layTable();
            }

            $data = [];

            $gameIdName = model('Game')->gameIdName();
            $channelIdName = model('Channel')->channelIdName();
            foreach ($select as $k=>$v) {
                $data[] = [
                    'my_user_id' => $v['my_user_id'],
                    'my_user_name' => input('my_user_name'),
                    'channel_id' =>  $v['channel_id']==1000 ? 'CPS' : 'SDK',
                    'cps_id' => $v['channel_id']==1000 ? $v['cps_id']: $v['channel_id'],
                    'cps_name' => $v['channel_id']==1000 ? $channelIdName[ $v['cps_id'] ] : $channelIdName[ $v['channel_id'] ],
                    'game_id' => $v['game_id'],
                    'game_name' => $gameIdName[ $v['game_id'] ],
                    'bond_time' => date('Y-m-d H:i:s', $v['bond_time']),
                ];
            }

            $this->layTable(0, '游戏记录', $data, $count);
        }else{
            return view();
        }
    }

    public function pwd(){
        if( request()->isAjax() ){
            $editRs = model('User')->save(['password'=>sha1(input('pwd'))], ['my_user_id'=>input('my_user_id')]);

            if($editRs!==false){
                $json['my_user_name'] = model('User')->where('my_user_id', input('my_user_id'))->value('my_user_name');
                $json['sign'] = md5($json['my_user_name'].'asdf654768j3okjsodiuflsdkfj02kjdk');
                http('http://sdk.miyuhaiwai.com/Flushuser/index', $json);

                $this->layJson(0, '修改成功');
            }else{
                $this->layJson(1, '修改失败，请重试');
            }
        }else{
            return view();
        }
    }
}